package com.springboot.demo.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.cx.item.common.exception.CommonException;
import com.cx.item.common.par.PageParam;
import com.cx.item.common.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.mapper.AttachmentFileMapper;
import com.springboot.demo.mapper.InformationMapper;
import com.springboot.demo.model.example.AttachmentFileExample;
import com.springboot.demo.model.example.InformationExample;
import com.springboot.demo.model.model.AttachmentFile;
import com.springboot.demo.model.model.Information;
import com.springboot.demo.model.par.InformationPar;
import com.springboot.demo.model.vo.InformationVo;
import com.springboot.demo.service.InformationBGService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by heavin on 2018/6/21.
 */
@Service
public class InformationBGServiceImpl extends BaseServiceImpl<Information, InformationExample, InformationMapper> implements InformationBGService {

    @Autowired
    private InformationMapper informationMapper;
    @Autowired
    private AttachmentFileMapper attachmentFileMapper;

    @Transactional
    @Override
    public int create(InformationPar informationPar) {
        Information information = new Information();
        BeanUtil.copyProperties(informationPar, information);
       /* String informationContent = information.getContent();
        if (StrUtil.isNotEmpty(informationContent)) {
            information.setContent(HtmlUtil.encode(informationContent));
        }*/
        information.setCreateTime(new Date());
        information.setInformationStatus(1);
        int result = informationMapper.insertSelective(information);
        //更新图片
        List<String> informationImgList = informationPar.getCreateImgList();
        if (CollUtil.isNotEmpty(informationImgList)) {
            attachmentFileMapper.updateSoureceIdAndTypeByPath(informationImgList, 1, information.getInformationId(), 1);
        }

        return result;

    }

    @Override
    public PageInfo<List<InformationVo>> queryList(PageParam pageParam) {

        InformationExample example = new InformationExample();
        InformationExample.Criteria criteria = example.createCriteria();
        criteria.andInformationStatusEqualTo(1);
        example.setOrderByClause("information_sort asc,create_time desc ");

        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());
        List<Information> informationList = informationMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo(informationList);

        List<InformationVo> informationVoList = new ArrayList<>();
        InformationVo informationVo;
        for (Information information : informationList) {
            informationVo = new InformationVo();
            BeanUtils.copyProperties(information, informationVo);
            //查询图片
            AttachmentFileExample fileExample = new AttachmentFileExample();
            AttachmentFileExample.Criteria fileExampleCriteria = fileExample.createCriteria();
            fileExampleCriteria.andFileStatusEqualTo(1);
            fileExampleCriteria.andSourceIdEqualTo(informationVo.getInformationId());
            fileExampleCriteria.andSourceTypeEqualTo(1);
            List<AttachmentFile> attachmentFileList = attachmentFileMapper.selectByExample(fileExample);

            List<String> informationImgList = new ArrayList<>();
            if (CollUtil.isNotEmpty(attachmentFileList)) {
                for (AttachmentFile file : attachmentFileList) {
                    informationImgList.add(file.getFilePath());
                }
                informationVo.setInformationImgList(informationImgList);
            }

            informationVoList.add(informationVo);
        }

        pageInfo.setList(informationVoList);
        return pageInfo;

    }

    @Override
    public int update(InformationPar informationPar) {
        Information information = new Information();
        BeanUtils.copyProperties(informationPar, information);
       /* String informationContent = information.getContent();
        if (StrUtil.isNotEmpty(informationContent)) {
            information.setContent(HtmlUtil.encode(informationContent));
        }*/
        information.setUpdateTime(new Date());
        int result = informationMapper.updateByPrimaryKeySelective(information);
        // 处理图片
        List<String> createImgList = informationPar.getCreateImgList();
        if (CollUtil.isNotEmpty(createImgList)) {
            attachmentFileMapper.updateSoureceIdAndTypeByPath(createImgList, 1, information.getInformationId(), 1);
        }
        List<String> delImgList = informationPar.getDelImgList();
        if (CollUtil.isNotEmpty(delImgList)) {
            attachmentFileMapper.updateSoureceIdAndTypeByPath(delImgList, 0, information.getInformationId(), 1);
        }


        return result;
    }

    @Override
    public int delById(Integer informationId) {
        Information information = new Information();
        information.setInformationId(informationId);
        information.setInformationStatus(0);
        return informationMapper.updateByPrimaryKeySelective(information);
    }

    @Override
    public InformationVo queryById(Integer id) {
        Information information = informationMapper.selectByPrimaryKey(id);
        if (null == information) {
            throw new CommonException("您查询的资讯不存在");
        }
        InformationVo informationVo = new InformationVo();
        BeanUtils.copyProperties(information, informationVo);
        // 图片
        AttachmentFileExample fileExample = new AttachmentFileExample();
        AttachmentFileExample.Criteria fileExampleCriteria = fileExample.createCriteria();
        fileExampleCriteria.andFileStatusEqualTo(1);
        fileExampleCriteria.andSourceIdEqualTo(id);
        fileExampleCriteria.andSourceTypeEqualTo(1);
        List<AttachmentFile> attachmentFileList = attachmentFileMapper.selectByExample(fileExample);

        List<String> informationImgList = new ArrayList<>();
        if (CollUtil.isNotEmpty(attachmentFileList)) {
            for (AttachmentFile file : attachmentFileList) {
                informationImgList.add(file.getFilePath());
            }
            informationVo.setInformationImgList(informationImgList);
        }

        return informationVo;
    }
}
